package com.zlzkj.app.controller;

import com.zlzkj.app.model.Authority;
import com.zlzkj.app.model.Role;
import com.zlzkj.app.service.PermissionService;
import com.zlzkj.app.service.RoleService;
import com.zlzkj.app.service.ShiroUserService;
import com.zlzkj.app.util.MapUtil;
import com.zlzkj.app.util.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zlzkj.core.base.BaseController.ajaxReturn;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ShiroUserService shiroUserService;
    @Autowired
    private PermissionService permissionService;


    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response, Integer nowPage){
        Map map = MapUtil.convert(request.getParameterMap());
        model.addAttribute("pageView",new PageView(roleService.findByMap(map,nowPage)));
        return "role/index";
    }

    /*
        添加角色
     */
    @RequestMapping("/create")
    public String create(Model model, HttpServletRequest request, HttpServletResponse response, Role role){
        return ajaxReturn(response,roleService.save(role));
    }
    /*
        更新角色
     */
    @RequestMapping("/update")
    public String update(Model model, HttpServletRequest request, HttpServletResponse response, Role role){
        return ajaxReturn(response,roleService.update(role));
    }
    /*
        删除角色
     */
    @RequestMapping("/delete")
    public String delete(Model model, HttpServletRequest request, HttpServletResponse response, String id){
        return ajaxReturn(response,roleService.delete(id));
    }
    /*
        上下页的刷新
    * */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String,Object> roleList(HttpServletRequest request,Integer nowPage){
        Map<String,Object> result = new HashMap<>();
        Map map = MapUtil.convert(request.getParameterMap());
        result.put("pageView",new PageView(roleService.findByMap(map,nowPage)));
        return result;
    }
    /*
        检查角色是否已经存在
     */
    @ResponseBody
    @RequestMapping("/check_name")
    public boolean checkName(String name,String originalname){
        Map<String,Object> reuslt = new HashMap<>();
        reuslt.put("name",name);
        System.out.println(roleService.findByMap(reuslt).size() <=0 || name.equals(originalname));
        return roleService.findByMap(reuslt).size() <=0 || name.equals(originalname);
    }
    /*
        创建角色的时候默认拥有的功能
     */
    @ResponseBody
    @RequestMapping(value = {"/to_create"})
    public Map<String, Object> toCreate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap();
        result.put("obj",new Role());
        List<Authority> authorities = new ArrayList<Authority>();
        //表示默认的打勾的权限，可以取消
        authorities.add(new Authority("1","device:list"));
        result.put("authorities",authorities);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_edit"})
    public Map<String, Object> toEdit(HttpServletRequest request, String  id) {
        Map<String, Object> result = new HashMap();
        result.put("obj", roleService.findById(id));
        result.put("authorities",permissionService.selectAuthorityByRoleId(id));
        return result;
    }
}
